<template>
	<el-dialog
		:title="titleMap[mode]"
		v-model="visible"
		:width="1048"
		destroy-on-close
		@closed="$emit('closed')"
	>
		<el-form
			:model="SelectRev"
			:rules="rules"
			ref="dialogForm"
			label-position="top"
			class="formhead"
		>
			<el-row style="display: flex; align-items: center">
				<el-col :span="12">
					<span style="font-size: 25px">{{ titleMap[mode] }}</span>
				</el-col>
				<el-col :span="2">
					<el-form-item label="色号" prop="main.code" class="backgroudGreen">
						<el-input
							label="色号"
							v-model="SelectRev.main.code"
							readonly
						></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="2">
					<el-form-item label="色名" prop="main.name" class="backgroudBlue">
						<el-input
							label="色号"
							v-model="SelectRev.main.name"
							clearable
						></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="2">
					<el-form-item
						label="客户"
						prop="SelectRev.main.company_id"
						class="backgroudGreen"
					>
						<el-input
							label="客户"
							v-model="SelectRev.main.company.name"
							readonly
						></el-input>
					</el-form-item>
				</el-col>

				<el-col :span="2">
					<el-form-item label="产品" prop="material_id" class="backgroudBlue">
						<skAutoChoose
							v-model="SelectRev.material.name"
							:name="'material'"
							:assign="SelectRev"
							:params="[{ material_nature_id: 3 }]"
						>
						</skAutoChoose>
					</el-form-item>
				</el-col>
				<el-col :span="2">
					<el-form-item
						label="客户色号"
						prop="main.code_en"
						class="backgroudGreen"
					>
						<el-input
							label="客户色号"
							v-model="SelectRev.main.code_en"
							clearable
						></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="2">
					<el-form-item
						label="客户色名"
						prop="main.code_en_name"
						class="backgroudBlue"
					>
						<el-input
							label="客户色名"
							v-model="SelectRev.main.code_en_name"
							clearable
						></el-input>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>

		<el-form
			:model="SelectRev"
			:rules="rules"
			ref="dialogForm"
			label-width="70px"
			label-position="right"
			class="el-tabs--border-card"
		>
			<el-row>
				<el-col :span="19">
					<el-row>
						<el-col :span="5">
							<el-form-item
								label="颜色深浅"
								prop="main.ColorShade_id"
							>
								<skDicSelect
									v-model="SelectRev.main.ColorShade_id"
									:code="'ColorShade'"
									:placeholder="'请选择颜色深浅'"
								>
								</skDicSelect>
							</el-form-item>
						</el-col>
						<el-col :span="5">
							<el-form-item label="批号" prop="lot_id">
								<skAutoChoose
									v-model="SelectRev.lot.name"
									:name="'lot'"
									:assign="SelectRev"
									:browserShow="false"
									:params="[
										{ material_id: SelectRev.material_id },
									]"
								>
								</skAutoChoose>
							</el-form-item>
						</el-col>

						<el-col :span="5">
							<el-form-item label="贸易色号" prop="code_my">
								<el-input
									label="贸易色号"
									v-model="SelectRev.code_my"
									clearable
								></el-input>
							</el-form-item>
						</el-col>

						<el-col :span="9">
							<el-form-item label="打样备注" prop="main.remark">
								<el-input
									v-model="SelectRev.main.remark"
									clearable
								></el-input>
							</el-form-item>
						</el-col>

						<el-col :span="5">
							<el-form-item label="色族" prop="main.colorgroup_id">
								<skDicSelect
									v-model="SelectRev.main.colorgroup_id"
									:code="'colorgroup'"
									:placeholder="'请选择色族'"
								>
								</skDicSelect>
							</el-form-item>
						</el-col>

						<el-col :span="5">
							<el-form-item label="成分" prop="comp_id">
								<skDicSelect
									v-model="SelectRev.comp_id"
									:code="'Comp'"
									:placeholder="'请选择成分'"
								>
								</skDicSelect>
							</el-form-item>
						</el-col>
						<el-col :span="5">
							<el-form-item label="浴法" prop="dyebath_id">
								<skDicSelect
									v-model="SelectRev.dyebath_id"
									:code="'Dyebath'"
									:placeholder="'请选择浴法'"
								>
								</skDicSelect>
							</el-form-item>
						</el-col>
						<el-col :span="9">
							<el-form-item label="工艺" prop="dyecraft.name">
								<skAutoChoose
									v-model="SelectRev.dyecraft.name"
									:name="'dyecraft'"
									@select="dyecraftSelect"
									:asKey="'rscode'"
									:asField="'code'"
									:assign="SelectRev"
									:prepend_width="'60px'"
								>
									<template #prepend>{{
										SelectRev.dyecraft.code
									}}</template>
								</skAutoChoose>
							</el-form-item>
						</el-col>
					</el-row>
				</el-col>
				<el-col :span="5">
					<el-row>
						<el-col :span="24">
							<el-form-item label="颜色" prop="main.rgb">
								<el-color-picker
									v-model="SelectRev.main.rgb"
									:predefine="predefineColors"
								></el-color-picker>
							</el-form-item>
						</el-col>
					</el-row>
				</el-col>
			</el-row>
		</el-form>
		<el-container>
			<el-aside style="width: 330px">
				<el-container>
					<el-main class="el-tabs--border-card">
						<sc-VxeTable
							:tablecode="'pro_color_rev'"
							:data="details"
							:gridOps="{ height: 450 }"
							:gridEvts="gridEvent"
							ref="grid"
						>
						</sc-VxeTable>
					</el-main>
				</el-container>
			</el-aside>
			<el-container>
				<el-main class="el-tabs--border-card">
					<sc-VxeTable
						:tablecode="'pro_color_rev_list'"
						:data="SelectRev.items"
						:gridOps="gridSubOpts"
						:gridEvts="gridSubEvt"
						ref="subbe"
						:sortable="true"
					>
					</sc-VxeTable>
				</el-main>
			</el-container>
		</el-container>

		<!-- </sc-water-mark> -->
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button type="primary" :loading="isSaveing" @click="submit()"
				>保 存</el-button
			>
		</template>
	</el-dialog>
</template>

<script>
import skDicSelect from "@/components/skDicSelect";
import skAutoChoose from "@/components/skAutoChoose";
import { ElMessageBox } from "element-plus";
// import XEUtils from "xe-utils";
export default {
	emits: ["success", "closed"],
	components: { skAutoChoose, skDicSelect },
	data() {
		return {
			mode: "add",
			gridOps: {
				height: 300,
			},
			gridEvent: {
				cellDblclick: ({ row }) => {
					this.initSev(row);
				},
				edit: ({ button }) => {
					this.initSev(button.selectRecords[0]);
				},
				del: ({ button }) => {
					ElMessageBox.confirm("您确定要删除该数据?")
						.then((type) => {
							if (type === "confirm") {
								return this.$API.pro.color.deleteRevision
									.post({
										ids: button.selectRecords.map(
											(item) => item.id
										),
									})
									.then((res) => {
										if (res.code == "200") {
											this.SelectRev = this.defaultRev;
											this.getItems(
												this.defaultRev.main_id
											);
										}
									});
							}
						})
						.catch(() => {});
				},
				copy: ({ button }) => {
					this.initSev(button.selectRecords[0], "add");
				},
				reset: () => {
					this.SelectRev = this.defaultRev;
				},
			},
			gridSubEvt: {},
			gridSubOpts: {
				height: 450,
				columns: [
					{
						field: "material.name",
						event: {
							select: ({ e, row }) => {
								if (e.name) {
									row["material"] = e;
									row["procedure"] = e.procedure;
									row["uunit"] = e.uunit ?? {};
									row["ounit"] = e.ounit ?? {};
									row["uunit_id"] = e.uunit_id;
									row["ounit_id"] = e.ounit_id;
									row["cost"] =
										row.material?.average_price *
										row["rate"] *
										(row["ounit_"] == 2 ? 1 : 10);
								}
							},
							keyup: ({ e, params }) => {
								let { $grid, _rowIndex, items, column } =
									params;
								this.enterAndSelect(
									e,
									_rowIndex,
									items,
									column,
									$grid
								);
							},
						},
					},
					{
						field: "rate",
						event: {
							keyup: ({ e, params, row }) => {
								let { $grid, _rowIndex, items, column } =
									params;
								row["cost"] =
									row.material?.average_price *
									row["rate"] *
									(row["ounit_"] == 2 ? 1 : 10);
								this.enterAndSelect(
									e,
									_rowIndex,
									items,
									column,
									$grid
								);
							},
						},
					},
				],
			},
			titleMap: {
				add: "新增",
				edit: "编辑",
			},
			predefineColors: [
				"#ff4500",
				"#ff8c00",
				"#ffd700",
				"#67C23A",
				"#00ced1",
				"#409EFF",
				"#c71585",
			],
			details: [],
			defaultRev: {
				material: {},
				lot: {},
				dyecraft: {},
				dyebath: {},
				comp: {},
				main: { company: {}, color_shade: {} },
				items: [{ id: "" }], //默认自带一行空数据
			},
			SelectRev: null,
			rules: {
				"main.ColorShade_id": {
					required: true,
					message: "请选择颜色深浅",
				},
				comp_id: { required: true, message: "请选择成分" },
				dyebath_id: { required: true, message: "请选择浴法" },
				"dyecraft.name": { required: true, message: "请选择工艺" },
				code: { required: true, message: "色号必填" },
				name: { required: true, message: "色名必填" },
				"main.rgb": { required: true, message: "颜色必选" },
			},
			visible: false,
			isSaveing: false,
		};
	},
	mounted() {
		this.SelectRev = this.defaultRev;
	},
	methods: {
		enterAndSelect(e, _rowIndex, items, column, $grid) {
			if (e.keyCode == 13) {
				if (_rowIndex == items.length - 1) {
					this.SelectRev.items.push([]);
				}

				$grid
					.setEditCell(
						this.SelectRev.items[_rowIndex + 1],
						column.property
					)
					.then(() => {
						var ele = document.getElementsByClassName("is-focus");
						if (ele.length > 0)
							ele[0].getElementsByTagName("input")[0].select();
					});
				$grid.refreshScroll();
				$grid.scrollToRow(this.SelectRev.items[_rowIndex + 1]);
			}
		},
		initSev(selectitem, mode = "edit") {
			let item = this.$TOOL.objCopy(selectitem);
			// selectitem.dyebath_id = selectitem.dyebath_id + "";
			this.mode = mode;
			let obj = this.$TOOL.removeNull(item, (key, newobj) => {
				if (key == "main") {
					newobj[key] = Object.assign(
						this.defaultRev.main,
						this.$TOOL.removeNull(newobj[key])
					);
				}
				if (mode == "add") {
					if (key == "id") delete newobj[key];
					if ("items" == key) {
						newobj[key].forEach((v) => {
							v["id"] = "key" + v["id"];
						});
					}
				}
			});

			this.SelectRev = Object.assign({}, this.defaultRev, obj);
		},

		open(mode = "add") {
			this.mode = mode;
			this.visible = true;
			return this;
		},
		dyecraftSelect(data) {
			this.SelectRev.dyecraft = data;
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					this.SelectRev.items = this.SelectRev.items.filter(
						(item) => {
							return item["material_id"] > 0 && item["rate"] > 0;
						}
					);
					var res = await this.$API.pro.color.save.post({
						revision: this.SelectRev,
						details: this.details,
					});
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit("success", this.form, this.mode);
						this.visible = false;
						this.$message.success("操作成功");
					}
				}
			});
		},
		async getItems(id) {
			var res = await this.$API.pro.color.details.get({
				filter: [{ main_id: id }],
			});
			if (res.code == 200) {
				this.details = res.data.rows;
			}
		},

		//表单注入数据
		setData(data) {
			this.defaultRev.main = Object.assign(
				this.defaultRev.main,
				this.$TOOL.removeNull(data)
			);
			this.defaultRev.material = data.material ?? {};
			this.defaultRev.material_id = data.material_id;
			this.defaultRev.main_id = data.id;
			this.getItems(data.id);
		},
	},
};
</script>

<style></style>
